const { Op } = require("sequelize");
const nodeInfoModel = require("../model/nodeInfo")

class nodeInfoService {
    // 查询所有节点信息,不包括已删除的
    async findAll() {
        return await nodeInfoModel.findAll({
            where: {
                isDelete: false
            }
        });
    }

    // 根据姓名查询节点信息
    async findByName(name) {
        return await nodeInfoModel.findAll({
            where: {
                name: {
                    [Op.like]: `%${name}%`
                },
                isDelete: false
            }
        });
    }

    // 分页查询,不包括已删除的
    async findPage(page, size) {
        return await nodeInfoModel.findAndCountAll({
            offset: (page - 1) * size * 1,
            limit: size * 1,
            where: {
                isDelete: false
            }
        });
    }
    // 新增节点
    async createNodeInfoService({ parentId, projectId, time, projectLeader, isComplete, fileId, fileUrl }) {
        return await nodeInfoModel.create({ parentId, projectId, time, projectLeader, isComplete, fileId, fileUrl });
    }
    // 删除节点
    async deleteNodeInfo(id) {
        return await nodeInfoModel.update({ isDelete: true }, {
            where: {
                id
            }
        });
    }
    // 根据id查询节点信息 不包括已删除的
    async findById(id) {
        return await nodeInfoModel.findAll(
            {
                where: {
                    id,
                    isDelete: false
                }
            }
        );
    }
    // 通过父节点id查询节点信息
    async findByParentId(parentId) {
        return await nodeInfoModel.findAll({
            where: {
                parentId,
                isDelete: false
            }
        });
    }
}

module.exports = nodeInfoService;
